export const interceptors = {
    request (url, method, params, header) {
        header = Object.assign({}, {'content-type': 'application/json'}, header);
        return {
            url: formatUrl(url),
            method: method,
            params: this.deleteValueUndefined(params),
            header: header,
            dataType: 'json',
            responseType: 'text'
        };
    },
    response (res) {
        uni.hideLoading();
        switch (res.statusCode) {
            case 200: break;
            case 501:
            case 502:
            case 503:
            case 504:
            case 500: {
                uni.showToast({icon: 'none', title: 'HTTP错误' + res.statusCode});
                break;
            }
            default: {
                uni.showToast({icon: 'none', title: '请求失败'});
                break;
            }
        }
    },
    deleteValueUndefined (obj) {
        for (let key in obj) {
            let value = obj[key];
            if (value === undefined) {
                delete obj[key];
            } else if (typeof value === 'object') {
                obj[key] = this.deleteValueUndefined(value);
            }
        }
        return obj;
    }
};

function formatUrl (url) {
    if (/^(http|https):\/\//.test(url) !== true) {
        return config.api_url + url;
    }
    return url;
}

const request = {
    create: (url, method, params, header) => {
        let config = interceptors.request(url, method, params, header);
        let token = uni.getStorageSync('accessToken');
        if (token) {
            config.header = Object.assign({}, config.header, {'Authorization': 'Bearer ' + token, 'content-type': 'application/json'});
        }
        return new Promise((resolve, reject) => {
            uni.request({
                url: config.url,
                data: config.params,
                header: config.header,
                method: config.method,
                dataType: config.dataType,
                responseType: config.responseType,
                success: (res) => {
                    interceptors.response(res);
                    if (res.statusCode === 200) {
                        resolve(res);
                    } else {
                        reject(res);
                    }
                },
                fail: (res) => {
                    interceptors.response(res);
                    reject(res);
                }
            });
        });
    },
    get: (url, params, header) => {
        return request.create(url, 'GET', params, header);
    },
    post: (url, params, header) => {
        return request.create(url, 'POST', params, header);
    },
    delete: (url, params, header) => {
        return request.create(url, 'DELETE', params, header);
    },
    head: (url, params, header) => {
        return request.create(url, 'HEAD', params, header);
    },
    options: (url, params, header) => {
        return request.create(url, 'OPTIONS', params, header);
    },
    put: (url, params, header) => {
        return request.create(url, 'PUT', params, header);
    },
    patch: (url, params, header) => {
        return request.create(url, 'PATCH', params, header);
    }
};

export default request;
